﻿Public Class NhaCungCapDA
    Private cn As DatabaseConnect
    Private sql As String

    Public Sub New()
        cn = New DatabaseConnect()
    End Sub

    Public Function InUp(ByVal ncc As NhaCungCapDT, ByVal flag As Boolean)
        Dim ma As String = ncc.MaNCC
        Dim ten As String = ncc.TenNCC
        Dim dienthoai As String = ncc.DienThoai
        Dim diachi As String = ncc.DiaChi
        Dim email As String = ncc.Email
        Dim id As Integer = ncc.ID
        '----------------------------------------------------'
        If flag = True Then
            sql = "INSERT INTO NhaCungCap(MaNCC,hotenNCC,dienthoai,diachi,email) VALUES('" + ma + "',N'" + ten + "','" + dienthoai + "',N'" + diachi + "','" + email + "')"
        Else
            sql = "UPDATE NhaCungCap SET hotenNCC = N'" + ten + "', dienthoai = '" + dienthoai + "', diachi = N'" + diachi + "', email = '" + email + "' WHERE MaNCC = '" + ma + "' "
        End If
        Return cn.Execute(sql)
    End Function

    Public Function Delete(ByVal ncc As NhaCungCapDT)
        Dim id As Integer = ncc.ID
        sql = "delete from NhaCungCap where Id = " & id
        Return cn.Execute(sql)
    End Function

    Public Function GetData(ByVal dieukien As String) As List(Of NhaCungCapDT)
        Dim id As Integer
        Dim ma, ten, dienthoai, diachi, email As String
        Dim ncc As NhaCungCapDT
        Dim list As New List(Of NhaCungCapDT)
        Dim dt As New DataTable
        dt = cn.GetData("SELECT * FROM NhaCungCap" + dieukien)

        For i As Integer = 0 To dt.Rows.Count() - 1
            id = Convert.ToInt32(dt.Rows(i)("Id"))
            ma = dt.Rows(i)("MaNCC").ToString()
            ten = dt.Rows(i)("hotenNCC").ToString()
            dienthoai = dt.Rows(i)("dienthoai").ToString()
            diachi = dt.Rows(i)("diachi").ToString()
            email = dt.Rows(i)("email").ToString()
            '-----'
            ncc = New NhaCungCapDT(ma, ten, dienthoai, diachi, email, id)
            list.Add(ncc)
        Next
        Return list
    End Function

    Function GetID() As Integer
        Dim dt As New DataTable
        Dim id As Integer
        dt = cn.GetData("SELECT * FROM NhaCungCap")
        If dt.Rows.Count > 0 Then
            id = Convert.ToInt32(dt.Rows(dt.Rows.Count - 1).Item("Id"))
        Else
            id = 1
        End If
        Return id
    End Function

    Public Function GetInserted() As String
        Dim dt As New DataTable
        Dim ma As String
        dt = cn.GetData("SELECT * FROM NhaCungCap")
        ma = dt.Rows(dt.Rows.Count - 1)("MaNCC").ToString()
        Return ma
    End Function
End Class
